﻿#include "datapass.h"
#include <QDir>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <QMessageBox>
#include <QDebug>
#include <QVariant>
#include <QTableView>
#include <QFile>
#include <QTextStream>

QByteArray bhz,dxpt,sxpt;
QByteArray SendData;
DATAPASS::DATAPASS(QWidget *parent) :
    QWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);//隐藏边框和标题栏
    usb = false;
}

void DATAPASS::strinit()
{
    bhz.resize(6);
    sxpt.resize(6);
    dxpt.resize(6);
    SendData.resize(222);
    int i=0;
    for(i=0;i<222;i++)
    {
        SendData[i]=0x00;
    }

    bhz[0]=0xB2;
    bhz[1]=0xBB;
    bhz[2]=0xBB;
    bhz[3]=0xAF;
    bhz[4]=0xD5;
    bhz[5]=0xFB;

    sxpt[0]=0xC8;
    sxpt[1]=0xFD;
    sxpt[2]=0xCF;
    sxpt[3]=0xE0;
    sxpt[4]=0x50;
    sxpt[5]=0x54;

    dxpt[0]=0xB5;
    dxpt[1]=0xA5;
    dxpt[2]=0xCF;
    dxpt[3]=0xE0;
    dxpt[4]=0x50;
    dxpt[5]=0x54;
}

bool DATAPASS::changeTransMethod(QString method)
{
    if(method == "Rs232")
    {
        usb=false;

        openserial();

        if(!serial->isOpen()) return false;
    }
    else if(method == "Udsik")
    {
        usb=true;

        if( !checkUdisk()) return false;
    }

    return true;
//    Sum = zh;
}
QString DATAPASS::findserial()
{
    int i=0;
    QFile *ser;
    for(i=0;i<5;i++)
    {
        ser = new QFile("/dev/ttyUSB" + QString::number(i));
        if(ser->exists()) break;
        delete ser;
        ser = NULL;
    }
    if(ser == NULL) return "";
    qDebug()<<"USB"<<ser->fileName();
    return ser->fileName();
}

void DATAPASS::openserial()
{
    QString str;
    QString portName;
    serial = new QSerialPort(this);
    if ((portName = findserial()) != "")
    {
        qDebug()<<portName;

        serial->setPortName(portName);
        serial->setBaudRate(QSerialPort::Baud9600);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->open(QIODevice::ReadWrite);
    }

    str = portName +"串口打开失败," + "请检查USB串口线连接,并重新点击开始！";
    qDebug()<<"The str is"<< str;

    if(serial->isOpen())
    {
        Opend = true;
        qDebug()<<"iiii";
    }
    else
    {
        QMessageBox::warning(this,tr("错误"),/*tr("/dev/ttyUSB串口打开失败！")*/str,QMessageBox::Ok);
        Opend = false;
    }
//    connect(serial,SIGNAL(readyRead()),this,SLOT(readData1()));
}

void DATAPASS::bbshow(QString str1,QString str2)
{
    str1 = str1.simplified();
    str2 = str2.simplified();
    str1.resize(str1.length()-1);
    str2.resize(str2.length()-1);
    qDebug()<<"the str is"<<str1<<"  "<<str2;
    if(str2 == "5")
    {
        if(str1 == "25000" || str1 == "3000" || str1 == "750" || str1 == "125" || str1 == "20")
        {
            SendData[30]=0x30;
            if(str1 == "25000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "3000")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "750")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "125")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "20")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "20000" || str1 == "2500" || str1 == "630" || str1 == "120" || str1 == "15")
        {
            SendData[30]=0x31;
            if(str1 == "20000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "2500")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "630")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "120")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "15")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "15000" || str1 == "2000" || str1 == "600" || str1 == "100" || str1 == "12.5")
        {
            SendData[30]=0x32;
            if(str1 == "15000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "2000")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "600")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "100")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "12.5")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "10000" || str1 == "1800" || str1 == "500" || str1 == "80" || str1 == "10")
        {
            SendData[30]=0x33;
            if(str1 == "10000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "1800")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "500")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "80")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "10")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "8000" || str1 == "1600" || str1 == "400" || str1 == "75" || str1 == "8")
        {
            SendData[30]=0x34;
            if(str1 == "8000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "1600")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "400")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "75")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "8")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "7500" || str1 == "1500" || str1 == "315" || str1 == "60" || str1 == "7.5")
        {
            SendData[30]=0x35;
            if(str1 == "7500")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "1500")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "315")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "60")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "7.5")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "6000" || str1 == "1250" || str1 == "300" || str1 == "50" || str1 == "6")
        {
            SendData[30]=0x36;
            if(str1 == "6000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "1250")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "300")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "50")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "6")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "5000" || str1 == "1200" || str1 == "250" || str1 == "40" || str1 == "5")
        {
            SendData[30]=0x37;
            if(str1 == "5000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "1200")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "250")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "40")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "5")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "4000" || str1 == "1000" || str1 == "200" || str1 == "30" || str1 == "2.5")
        {
            SendData[30]=0x38;
            if(str1 == "4000")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "1000")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "200")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "30")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "2.5")
            {
                SendData[31]=0x34;
            }
        }
        else if(str1 == "3150" || str1 == "800" || str1 == "150" || str1 == "25" || str1 == "2")
        {
            SendData[30]=0x39;
            if(str1 == "3150")
            {
                SendData[31]=0x30;
            }
            else if(str1 == "800")
            {
                SendData[31]=0x31;
            }
            else if(str1 == "150")
            {
                SendData[31]=0x32;
            }
            else if(str1 == "25")
            {
                SendData[31]=0x33;
            }
            else if(str1 == "2")
            {
                SendData[31]=0x34;
            }
        }
    }
    else if(str2 == "1")
        {
            if(str1 == "5000" || str1 == "1200" || str1 == "250" || str1 == "40")
            {
                SendData[30]=0x30;
                if(str1 == "5000")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "1200")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "250")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "40")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "4000" || str1 == "1000" || str1 == "200" || str1 == "30")
            {
                SendData[30]=0x31;
                if(str1 == "4000")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "1000")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "200")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "30")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "3150" || str1 == "800" || str1 == "150" || str1 == "25")
            {
                SendData[30]=0x32;
                if(str1 == "3150")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "800")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "150")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "25")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "3000" || str1 == "750" || str1 == "125" || str1 == "20")
            {
                SendData[30]=0x33;
                if(str1 == "3000")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "750")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "125")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "20")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "2500" || str1 == "630" || str1 == "120" || str1 == "15")
            {
                SendData[30]=0x34;
                if(str1 == "2500")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "630")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "120")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "15")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "2000" || str1 == "600" || str1 == "100" || str1 == "12.5")
            {
                SendData[30]=0x35;
                if(str1 == "2000")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "600")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "100")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "12.5")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "1800" || str1 == "500" || str1 == "80" || str1 == "10")
            {
                SendData[30]=0x36;
                if(str1 == "1800")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "500")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "80")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "10")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "1600" || str1 == "400" || str1 == "75" || str1 == "8")
            {
                SendData[30]=0x37;
                if(str1 == "1600")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "400")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "75")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "8")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "1500" || str1 == "315" || str1 == "60" || str1 == "6")
            {
                SendData[30]=0x38;
                if(str1 == "1500")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "315")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "60")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "6")
                {
                     SendData[31]=0x33;
                }
            }
            else if(str1 == "1250" || str1 == "300" || str1 == "50" || str1 == "5")
            {
                SendData[29]=0x39;
                if(str1 == "1250")
                {
                    SendData[31]=0x30;
                }
                else if(str1 == "300")
                {
                     SendData[31]=0x31;
                }
                else if(str1 == "50")
                {
                     SendData[31]=0x32;
                }
                else if(str1 == "5")
                {
                     SendData[31]=0x33;
                }
            }
        }
}

void DATAPASS::deal_data(QStringList list)
{    
    qDebug()<<"The list is"<<list;
    QByteArray cache;
    QStringList m,strlist;
    QString str;
    QString shu,cs;
    int i=0,j=0,k=0;
    int len=0;
    for(i=0;i<list.length();i++)
    {
        if(i != 1)
        {
            str = list.at(i);
            str = str.simplified();
            len = str.length();
        }
        if(i == 3) //bb
        {
            SendData[40]=0x30;
            strlist = str.split("/");
            for(int i=0;i<strlist.count();i++)
            {
                m<<strlist.at(i);
            }
            shu=m[0];
            cs=m[1];
            bbshow(shu,cs);

            cs = cs.simplified();
            cs.resize(cs.length()-1);

            if(cs == "5")
            {
                SendData[41]=0x30;
            }
            else if(cs == "1")
            {
                SendData[41]=0x31;
            }
        }
        else if(i == 4) //jd
        {
            qDebug()<<"The jd is:"<<str;
            SendData[32]=0x30;
            QString str1;
            for(int d= 0;d<str.length();d++)
            {
                if(str[d] != ' ')
                {
                    str1.append(str[d]);
                }
            }
            qDebug()<<"changed jd is:"<<str1;
            if(str1 == "0.2")
            {
                SendData[33]=0x30;
            }
            else if(str1 == "0.5")
            {
                SendData[33]=0x31;
            }
            else if(str1 == "1.0")
            {
                SendData[33]=0x32;
            }
            else if(str1 == "0.2s")
            {
                SendData[33]=0x33;
            }
            else if(str1 == "0.5s")
            {
                SendData[33]=0x34;
            }
            else if(str1 == "不化整")
            {
                SendData[33]=0x35;
            }
        }
        else if(i == 5) //glys
        {
            qDebug()<<"power is"<<str;
            SendData[38]=0x30;
            if(str == "1.0")
            {
                SendData[39]=0x30;
            }
            else if(str == "0.9")
            {
                SendData[39]=0x31;
            }
            else if(str == "0.8")
            {
                SendData[39]=0x32;
            }
            else if(str == "0.7")
            {
                SendData[39]=0x33;
            }
            else if(str == "0.6")
            {
                SendData[39]=0x34;
            }
            else if(str == "0.5")
            {
                SendData[39]=0x35;
            }
            else if(str == "0.4")
            {
                SendData[39]=0x36;
            }
            else if(str == "0.3")
            {
                SendData[39]=0x37;
            }
            else if(str == "0.2")
            {
                SendData[39]=0x38;
            }
            else if(str == "0.1")
            {
                SendData[39]=0x39;
            }
        }
        else if(i == 6) //edfh
        {
            str.resize(str.length()-2);
            qDebug()<<"edfh"<<str;
            if(str != "100")
            {
                SendData[34]=0x30;
                if(str == "5")
                {
                    SendData[35]=0x30;
                }
                else if(str == "10")
                {
                    SendData[35]=0x31;
                }
                else if(str == "15")
                {
                    SendData[35]=0x32;
                }
                else if(str == "20")
                {
                    SendData[35]=0x33;
                }
                else if(str == "25")
                {
                    SendData[35]=0x34;
                }
                else if(str == "30")
                {
                    SendData[35]=0x35;
                }
                else if(str == "40")
                {
                    SendData[35]=0x36;
                }
                else if(str == "50")
                {
                    SendData[35]=0x37;
                }
                else if(str == "60")
                {
                    SendData[35]=0x38;
                }
                else if(str == "80")
                {
                    SendData[35]=0x39;
                }

            }
            else
            {
                SendData[34]=0x31;
                SendData[35]=0x30;
            }
        }
        else if(i == 17) //xxfh
        {
            str.resize(str.length()-2);
            if(str != "20" && str != "25")
            {
                SendData[36]=0x30;
                if(str == "1")
                {
                    SendData[37]=0x30;
                }
                else if(str == "1.25")
                {
                    SendData[37]=0x31;
                }
                else if(str == "2.5")
                {
                    SendData[37]=0x32;
                }
                else if(str == "3.75")
                {
                    SendData[37]=0x33;
                }
                else if(str == "5")
                {
                    SendData[37]=0x34;
                }
                else if(str == "6.25")
                {
                    SendData[37]=0x35;
                }
                else if(str == "7.5")
                {
                    SendData[37]=0x36;
                }
                else if(str == "10")
                {
                    SendData[37]=0x37;
                }
                else if(str == "12.5")
                {
                    SendData[37]=0x38;
                }
                else if(str == "15")
                {
                    SendData[37]=0x39;
                }

            }
            else
            {
                SendData[36]=0x31;
                if(str == "20")
                {
                    SendData[37]=0x30;
                }
                else if(str == "25")
                {
                    SendData[37]=0x31;
                }
            }
        }
        else
        {
            for(k=0;k<len;k++)
            {
                if((str[k]>='0' && str[k]<='9') /*|| (str[i] >= 'A' && str[i] <= 'Z')*/)
                {
                    cache.append(str[k].toAscii());
                }
                else if(str[k]=='.')
                {
                    cache.append(0x2E);
                }
                else if(str[k]=='-')
                {
                    cache.append(0x2D);
                }
                else if(str[k]=='/')
                {
                    cache.append(0x2F);
                }
                else
                {
                    cache.append(0x20);
                }
            }
            if(i == 0) //bh
            {
                if(cache.length()<18)
                {
                    for(k = cache.length();k < 18;k ++)
                    {
                        cache.append(0x20);
                    }
                }
                for(k=0,j=0;k<18,j<18;k++,j++)
                {
                    SendData[j] = cache[k];
                }
                cache.clear();
            }

            else if(i == 2)//time
            {
                for(k=0,j=18;k<10,j<30;k++,j++)
                {
                    SendData[j] = cache[k];
                }
                SendData[28] = 0x20;
                SendData[29] = 0x20;
                cache.clear();
            }

            else
            {
                if(cache.length()<10)
                {
                    for(k=cache.length();k<10;k++)
                    {
                        cache.append(0x20);
                    }
                }
                if(i == 8) //edf5
                {
                    for(k=0,j=42;k<10,j<52;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 13) //edk5
                {
                    for(k=0,j=52;k<10,j<62;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 9) //edf20
                {
                    for(k=0,j=62;k<10,j<72;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 14) //edk20
                {
                    for(k=0,j=72;k<10,j<82;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 10) //edf100
                {
                    for(k=0,j=82;k<10,j<92;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 15) //edk100
                {
                    for(k=0,j=92;k<10,j<102;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 11) //edf120
                {
                    for(k=0,j=102;k<10,j<112;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 16) //edk120
                {
                    for(k=0,j=112;k<10,j<122;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 19) //xxf5
                {
                    for(k=0,j=122;k<10,j<132;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 23) //xxk5
                {
                    for(k=0,j=132;k<10,j<142;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 20) //xxf20
                {
                    for(k=0,j=142;k<10,j<152;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 24) //xxk20
                {
                    for(k=0,j=152;k<10,j<162;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 21) //xxf100
                {
                    for(k=0,j=162;k<10,j<172;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 25) //xxk100
                {
                    for(k=0,j=172;k<10,j<182;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 7) //edf1
                {
                    for(k=0,j=182;k<10,j<192;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 12) //edk1
                {
                    for(k=0,j=192;k<10,j<202;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 18) //xxf1
                {
                    for(k=0,j=202;k<10,j<212;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                else if(i == 22) //xxk1
                {
                    for(k=0,j=212;k<10,j<222;k++,j++)
                    {
                        SendData[j]=cache[k];
                    }
                }
                cache.clear();
            }
        }
    }
    cache.clear();

    SendData[222]=0x21;

    sendData();
}

int DATAPASS::usbpass()
{
//    int zh = 0;
#ifndef i386
    QFile file("/udisk/CT.txt");
#else
    QFile file("./udisk/CT.txt");
#endif

    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::warning(this,"错误",tr("错误，未插入U盘或该U盘存储空间不足!"),QMessageBox::Yes);
        return -1;
    }

    return 0;
}

void DATAPASS::sendData()
{
#ifndef i386
    QFile file("/udisk/CT.txt");
#else
    QFile file("./udisk/CT.txt");
#endif

    if(usb == false)
    {
        if(serial->isOpen())
        {
            serial->write(SendData);
        }
    }
    else if(usb == true)
    {
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text|QIODevice::Append))
        {
            QMessageBox::warning(this,"错误",tr("错误，未插入U盘或该U盘存储空间不足!"),QMessageBox::Yes);
        }
        else
        {
//            ioToUdisk.setDevice(&file);

//            ioToUdisk<< SendData;

            file.write(SendData);

            qDebug()<<"SendData"<<SendData;

            file.close();
        }
    }

    SendData.clear();
}

//void DATAPASS::select_data(int i)
//{
//    QSqlDatabase db1 = QSqlDatabase::database("connection");
//    db1.setDatabaseName("my.db");
//    db1.open();

//    QSqlQuery as((QString("select * from shuju where ID='%1'").arg(i)),db1);

//    if(as.next())
//    {
//        dataselect(as,1);
//    }
//    else
//    {
//        return;
//    }
//    db1.close();
//}

bool DATAPASS::checkUdisk()
{
#ifndef i386
    QDir dir("/udisk");
#else
    QDir dir("./udisk");
#endif

    if(dir.exists())
    {
        int ret = usbpass();

        if(ret == 0)
        {
            qDebug()<<"Udisk find";

            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        QMessageBox::warning(this,"错误",tr("错误，请先插入 U 盘!"),QMessageBox::Yes);

        return false;
    }
}





































































































































































































































































































